package cn.sjx.graduate.util;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * @Date 2021/10/31 18:57
 * @Version 1.0
 * @Description
 */
public class TokenUtil {

    private TokenUtil() {}

    public static String getToken(HttpServletRequest req) {
        String token = req.getParameter("token");
        if (StringUtils.isBlank(token)) {
            return getTokenHeader(req);
        }
        return token;
    }

    public static String getTokenHeader(HttpServletRequest req) {
        if (req != null) {
            String token = req.getHeader("token");
            if (StringUtils.isNotBlank(token)) {
                return token;
            }
        }
        return getTokenCookie(req);
    }

    public static String getTokenCookie(HttpServletRequest req) {
        if (req != null) {
            for (Cookie cookie : req.getCookies()) {
                if (cookie.getName().equals("token")) {
                    return cookie.getValue();
                }
            }
        }
        return null;
    }

}
